/************************************
作用：验证浮点数的存储形式:f= 1.M * 2^(E-127) * (-1)^S 
  而且M和S一般是小端bit序(和人脑相反)
Architecture:32bit_X86
Author:LysLee
************************************/

//知识点
/*******************************
科学计数法: Lbase.xxxx... * Base^N //0 < Lbase < Base
  因此对于二进制形式的科学计数(Base=2)来说Lbase始终为1
********************************/

#include <stdio.h>
#include "bitprint.h"


int main() {
  union ufloat {
    struct {
      unsigned M: 23; //Mantissa:科学计数形式的小数部分[0,22].df:52
      unsigned E : 8; //Exponent:指数[23,30]。df:11
      unsigned S : 1; //Sign:符号位(31)
    }fs;
    float f;
  };
  union ufloat uf1;
  uf1.f = 128.101f; 
  //10000000.0001100111011011001000101101.. 
  //=1.00000000001100111011011 * 2^(7=134-127)。df: -1023
  //134 = 10000110
  printf("M:%#X,E:%#X,S:%#X\n",uf1.fs.M,uf1.fs.E,uf1.fs.S);
  printf("%f = %s\n",uf1.f,"1.00000000001100111011011(2) * 2^7");
  bitprintf(uf1.f); 
  uf1.f = 0.; 
  printf("0:M=0,E=0,S=0-->1.000..* 2^(0-127)*(-1)^0\n");
  bitprintf(uf1.f); 
}
